<?php
/***
 * @name : 牌号厂家api
 * @Class : BrandAction
 * @Author : yu
 * @Create : 2016-10-28
 * @copyright : Fiidee.Monda.Cn
 */
namespace product\action;

use herosphp\core\Log;
use herosphp\http\HttpRequest;
use herosphp\utils\AjaxResult;
use herosphp\bean\Beans;
use api\utils\SaasApiClient;
/**--tmpl-use--**/

class BrandAction extends \admin\action\CommonAction {

	public $domain;

	public function __construct()
	{
		/*$app = WebApplication::getInstance();

		$appConfigs = $app->getConfigs();

		$this->domain = $appConfigs['spot_dayi35_com_api_url'];
*/
		$this->domain ='';
	}

	/**
	 * 类型
	 * @return AjaxResult
	 */
	public function type(HttpRequest $request)
	{

		$url = "api/brand/getTypes";
		$result = SaasApiClient::send($url, '');

		if ($result->isSucess()) {

			echo json_encode($result->getData());

		}else{
            //20170208 By Wong 当为空或者数据异常返回空数据
            Log::info($result->getMessage());
            echo json_encode(array());
        }
		die;
	}

	/**
	 * 品名
	 * @param $type_id 类型id
	 * @return AjaxResult
	 */
	public function name(HttpRequest $request)
	{
		$type_id = $request->getParameter('type_id');
		$url = "api/brand/getProducts";
		$result = SaasApiClient::send($url, array('typeId' => $type_id));
		if ($result->isSucess()) {

			echo json_encode($result->getData());

		}else{
            //20170208 By Wong 当为空或者数据异常返回空数据
            Log::info($result->getMessage());
            echo json_encode(array());
        }
		die;
	}

	/**
	 * 厂家
	 * @param $type_id  类型id
	 * @param $product_id 产品id
	 * @return AjaxResult
	 */
	public function factory(HttpRequest $request)
	{
		$type_id = $request->getParameter('type_id');
		$product_id = $request->getParameter('product_id');

		$url = "api/brand/getFactorys";
		$result = SaasApiClient::send($url, array('typeId' => $type_id, 'productId' => $product_id));
		if ($result->isSucess()) {

			echo json_encode($result->getData());

		}else{
            //20170208 By Wong 当为空或者数据异常返回空数据
            Log::info($result->getMessage());
            echo json_encode(array());
        }
		die;

	}

	/**
	 * 牌号
	 * @param $type_id 类型id
	 * @param $product_id 产品id
	 * @param $factory_id 厂家id
	 * @return AjaxResult
	 */
	public function brand(HttpRequest $request)
	{
		$type_id = $request->getParameter('type_id');
		$product_id = $request->getParameter('product_id');
		$factory_id = $request->getParameter('factory_id');

		$url = "api/brand/getBrands";
		$result = SaasApiClient::send($url, array('typeId' => $type_id, 'productId' => $product_id, 'factoryId' => $factory_id));
		if ($result->isSucess()) {

			echo json_encode($result->getData());

		}else{
		    //20170208 By Wong 当为空或者数据异常返回空数据
		    Log::info($result->getMessage());
		    echo json_encode(array());
        }
		die;

	}
	public function getCache($key)
	{
		$cache = CacheFactory::create();
		$data = $cache->get($key);

		if (!$data) {
			$cache->set($key, $data, 60 * 5);
		}
		return $data;
	}


	public function setCache($data, $key)
	{
		$cache = CacheFactory::create();
		$cache->set($key, $data, 60 * 5);
	}
	
	

}
